import { reactive, toRefs, getCurrentInstance, h } from "vue"
// ant design
import { message } from "ant-design-vue"
// api
import { booklists, bookdelete, isShelves, isOut, categoryStatus, bookdetailed } from "@/api/index"

export function useBook() {
    // proxy
    const { proxy } = getCurrentInstance()
    // state
    const state = reactive({
        dataList: [],
    })

    /**
     * 获取列表
     */
    const useGetBookList = (params) => {
        return new Promise((resolve, reject) => {
            booklists(params).then(res => {
                state.dataList = res?.content?.data
                resolve(res)
            })
        })
    }

    /**
     * 删除
     */
    const useDeleteBook = (row) => {
        proxy.$confirm({
            title: "确认删除？",
            content: "是否确认该书籍?",
            okAsync: () => {
              return new Promise((resolve, reject) => { // then，catch
                bookdelete({book_id: row.book_id}).then(() => {
                  message.success('删除成功')
                  useGetBookList()
                  resolve()
                }).catch(() => {
                  reject()
                })
              })
            }
          })
    }

    /**
     * 上架
     */
    const useIsShelves = (params) => {
        return new Promise((resolve, reject) => {
            isShelves({book_id: params.book_id}).then((res) => {
                message.success(res?.msg)
                useGetBookList()
                resolve()
            }).catch(() => {
                reject()
            })
        })
    }

    /**
     * 下架
     */
    const useIsOut = (row) => {
        proxy.$confirm({
            title: "确认下架？",
            content: "是否确认下架该书籍?",
            okAsync: () => {
              return new Promise((resolve, reject) => { // then，catch
                isOut({book_id: row.book_id}).then(() => {
                  message.success('下架成功')
                  useGetBookList()
                  resolve()
                }).catch(() => {
                  reject()
                })
              })
            }
          })
    }



    /**
     * 更新状态
     */
    const useStatusLiterature = (params) => {
        return new Promise((resolve, reject) => {
            const req = {
                category_id: params.category_id,
                category_status: params.category_status ? 0 : 1
            }
            categoryStatus(req).then((res) => {
                message.success(res?.msg)
                resolve()
            }).catch(() => {
                reject()
            })
        })
    }

    const useBookDetailed = (bookId) => {
      return new Promise((resolve, reject) => {
        bookdetailed({id: bookId}).then(res => {
          resolve(res)
        }).catch(() => {
          reject()
        })
      })
      
    }

    return {
        ...toRefs(state),
        useGetBookList,
        useDeleteBook,
        useStatusLiterature,
        useIsShelves,
        useIsOut,
        useBookDetailed
    }
}